package hr.dario.kosh;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;

import hr.dario.kosh.database.DataBaseAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.Toast;

public class LocDescActivity extends Activity {

	private DataBaseAdapter mDbHelper;

	private TextView mNameText;
	private TextView mDescText;
	private Long mRowId;
	private ListView mLv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_desc);
		
		mDbHelper = new DataBaseAdapter(this);
		

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setHomeAction(new IntentAction(this, createIntent(this),
				R.drawable.ic_title_home_default));
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		Gallery gallery = (Gallery) findViewById(R.id.gallery_loc_desc);
	    gallery.setAdapter(new ImageAdapter(this));

	    gallery.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	            Toast.makeText(LocDescActivity.this, "" + position, Toast.LENGTH_SHORT).show();
	        }
	    });
	    
		mLv = (ListView) findViewById(android.R.id.list);
		mLv.setEmptyView(findViewById(android.R.id.empty));
		mNameText = (TextView) findViewById(R.id.text_loc_desc1);
		mDescText = (TextView) findViewById(R.id.text_loc_desc2);	

		mDbHelper.open();
		Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mRowId = extras.getLong(DataBaseAdapter.KEY_LOC_ID);
        }
        mDbHelper.close();
        
		populateFields();
		fillData();
		
	}

	public static Intent createIntent(Context context) {
		Intent i = new Intent(context, ZujanjeActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		return i;
	}
	
	private void populateFields() {
		if (mRowId != null) {
			mDbHelper.open();
			Cursor note = mDbHelper.fetchInput(
					DataBaseAdapter.DATABASE_TABLE_LOC, mRowId);
			startManagingCursor(note);
			
			mNameText.setText(note.getString(note
					.getColumnIndex(DataBaseAdapter.KEY_LOC_NAME)));
			mDescText
					.setText(note.getString(note
							.getColumnIndex(DataBaseAdapter.KEY_LOC_DESCRIPTION)));
			mDbHelper.close();

		}
	}
	

	private void fillData() {
		// Get all of the rows from the database and create the item list
		mDbHelper.open();
		Cursor EventsCursor = mDbHelper
				.fetchAllInputs(DataBaseAdapter.DATABASE_TABLE_EVENT);
		startManagingCursor(EventsCursor);

		// Create an array to specify the fields we want to display in the list
		// (only TITLE)
		String[] from = new String[] { DataBaseAdapter.KEY_EVENT_NAME,
				DataBaseAdapter.KEY_EVENT_DESCRIPTION };

		// and an array of the fields we want to bind those fields to (in this
		// case just text1)
		int[] to = new int[] { R.id.text1, R.id.text2 };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter events = new SimpleCursorAdapter(this,
				R.layout.location_row, EventsCursor, from, to);
		mLv.setAdapter(events);
		mDbHelper.close();
	}

	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;

	    private Integer[] mImageIds = {
	            R.drawable.sample_1,
	            R.drawable.sample_2,
	            R.drawable.sample_3,
	            R.drawable.sample_4,
	            R.drawable.sample_5,
	            R.drawable.sample_6,
	            R.drawable.sample_7
	    };

	    public ImageAdapter(Context c) {
	        mContext = c;
	        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
	        mGalleryItemBackground = attr.getResourceId(
	                R.styleable.HelloGallery_android_galleryItemBackground, 0);
	        attr.recycle();
	    }

	    public int getCount() {
	        return mImageIds.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView = new ImageView(mContext);

	        imageView.setImageResource(mImageIds[position]);
	        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
	        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	        imageView.setBackgroundResource(mGalleryItemBackground);

	        return imageView;
	    }
	}
	


}
